/**
 * Copyright &copy; 2013-2015 山东易科德软件有限公司   All rights reserved.
 */
package cn.net.ecode.modules.msg.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.net.ecode.common.config.Global;
import cn.net.ecode.common.persistence.Page;
import cn.net.ecode.common.utils.StringUtils;
import cn.net.ecode.common.web.BaseController;
import cn.net.ecode.modules.msg.entity.Message;
import cn.net.ecode.modules.msg.entity.MessageRecord;
import cn.net.ecode.modules.msg.service.MessageService;

/**
 * 消息管理Controller
 * @author ThinkGem
 * @version 2015-5-30
 */
@Controller
@RequestMapping(value = "${adminPath}/msg/message")
public class MessageController extends BaseController {

	@Autowired
	private MessageService messageService;

	/**
	 * 获取数据
	 * @param id
	 * @return
	 */
	@ModelAttribute
	public Message get(String id, boolean isNewRecord) {
		return messageService.get(id, isNewRecord);
	}

	/**
	 * 查询列表
	 * @param message
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"list", ""})
	public String list(Message message, Model model) {
//		if (StringUtils.isBlank(message.getType())){
//			message.setType(Message.TYPE_MESSAGE);
//		}
		return "modules/msg/messageList";
	}

	/**
	 * 查询列表
	 * @param message
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value = "listData")
	public Page<Message> listData(Message message, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Message> page = messageService.findPage(new Page<Message>(request, response), message);
		return page;
	}

	/**
	 * 查看编辑表单
	 * @param message
	 * @param model
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(Message message, Model model) {
		if (StringUtils.isBlank(message.getId())) {
			message.setStatus(Message.STATUS_DRAFT);;
		}
		if (StringUtils.isBlank(message.getType())) {
			message.setType(Message.TYPE_MESSAGE);
		}
		if (StringUtils.isBlank(message.getReceiverType())) {
			message.setReceiverType(Message.RECEIVER_TYPE_USER);
		}
//		MessageUtils.send("你有新任务需要办理。", "消息来与测试，请尽快办理，谢谢！", "system", "系统管理员",
//				new String[][]{{"处理","/sys/user/list"}});
//		
//		MessageUtils.sendSms(null, "这是一条测试短信信息！" + DateUtils.getDateTime(),
//				Message.RECEIVER_TYPE_NONE, "18553130030", "王震");
//
//		MessageUtils.sendSms(null, "这是一条测试短信信息！by userCode " + DateUtils.getDateTime(),
//				Message.RECEIVER_TYPE_USER, "wangzhen", "王震");
//
//		MessageUtils.sendEmail(null, "这是一条测试邮件信息！ " + DateUtils.getDateTime(),
//				Message.RECEIVER_TYPE_NONE, "thinkgem@163.com", "王震");
//		
//		MessageUtils.sendWeixin(null, "这是一条测试邮件信息！ " + DateUtils.getDateTime(),
//				Message.RECEIVER_TYPE_NONE, "thinkgem@163.com", "王震");
		model.addAttribute("message", message);
		return "modules/msg/messageForm";
	}

	/**
	 * 查看消息
	 * @param message
	 * @param model
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "view")
	public String view(Message message, String mrId, Model model) {
		if (StringUtils.isBlank(message.getType())) {
			message.setType(Message.TYPE_MESSAGE);
		}
		/*if (StringUtils.isBlank(message.getReceiverType())){
			message.setReceiverType(Message.RECEIVER_TYPE_USER);
		}*/
		message = messageService.getReadMsg(message, mrId);
		if (message != null && StringUtils.isNotBlank(message.getId())) {
			MessageRecord messageRecord = new MessageRecord(message);
			messageRecord.setStatus(null); // 获取全部消息记录
			message.setMessageRecordList(messageService.findMessageRecordList(messageRecord));
			message.setMsgStatis(messageService.getMsgStatis(message));
		}
		model.addAttribute("message", message);
		return "modules/msg/messageView";
	}

	/**
	 * 保存数据
	 * @param message
	 * @return
	 */
	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value = "save")
	public String save(Message message) {
		StringBuilder resultMessage = new StringBuilder();
		if (!beanValidator(resultMessage, message)) {
			return renderResult(Global.FALSE, resultMessage);
		}
		messageService.save(message);
		String msg = "保存消息成功！";
		if (message.getStatus().equals(Message.STATUS_NORMAL)) {
			msg = "发送消息成功！";
		}
		return renderResult(Global.TRUE, msg);
	}

	/**
	 * 消息客户端接口（传递JSON参数）
	 * @return
	 */
	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value = "saveRecordByJson")
	public String saveRecordByJson(@RequestBody MessageRecord messageRecord) {
		StringBuilder resultMessage = new StringBuilder();
		if (!beanValidator(resultMessage, messageRecord)) {
			return renderResult(Global.FALSE, resultMessage);
		}
		if (!beanValidator(resultMessage, messageRecord.getMessage())) {
			return renderResult(Global.FALSE, resultMessage);
		}
		messageService.saveRecord(messageRecord);
		return renderResult(Global.TRUE, "传输消息记录成功！");
	}

	/**
	 * 删除数据
	 * @param message
	 * @return
	 */
	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value = "delete")
	public String delete(Message message, RedirectAttributes redirectAttributes) {
		if (!message.getCurrentUser().getUserCode().equals(message.getCreateByCode())) {
			return renderResult(Global.FALSE, "删除失败，只能删除自己发送的消息！");
		}
		messageService.delete(message);
		return renderResult(Global.TRUE, "删除消息成功！");
	}

	/**
	 * 即时聊天
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "chat")
	public String chat(HttpServletRequest request, HttpServletResponse response) {
		return "/modules/msg/messageChat";
	}

}